<?php

namespace App\Services;

use App\Exceptions\BobException;
use App\Http\Controllers\Drivers\V2\Common\UploadFileRepository;
use Illuminate\Support\Facades\Storage;

class UploadService extends BaseService
{
    protected $repository;

    /**
     * UploadFileService constructor.
     */
    public function __construct()
    {
        $this->repository = new UploadFileRepository();
    }

    /**
     * 上传图片
     *
     * @param $file
     * @return array|bool
     * @throws BobException
     * @author: Bob(bobcoder@qq.com)
     */
    public function upload($file)
    {
        //上传文件最大大小,单位M
        $maxSize = 10;
        //支持的上传图片类型
        $allowed_extensions = ["png", "jpg", "gif", "jpeg"];
        if (!$file) {
            return_bob('请选择需要上传的文件！');
        }
        //检查文件是否上传完成
        if ($file->isValid()) {
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                return_bob("请上传" . implode(",", $allowed_extensions) . "格式的图片");
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                return_bob("图片大小限制" . $maxSize . "M");
            }
        } else {
            return_bob($file->getErrorMessage());
        }
        $newFile = date('Y-m-d') . "_" . time() . "_" . uniqid() . "." . $file->getClientOriginalExtension();
        $disk = Storage::disk('oss');
        $disk->put($newFile, file_get_contents($file->getRealPath()));

        $image_id = $this->repository->insertGetId($newFile, $file->getClientSize(), $ext);

        return [
            'data' => $newFile,
            'file_id' => $image_id,
            'url' => $disk->getUrl($newFile)
        ];
    }

    /**
     * 上传文件
     * @param $file
     * @return array|bool
     * @author 姿势就是力量
     */
    public function uploadFile($file)
    {
        if (!$file) return $this->setError('请选择需要上传的文件！');

        // 上传文件最大大小,单位M
        $maxSize = 10;

        // 支持的上传文件类型
        $allowed_extensions = ["doc", "docx", "xls", "xlsx", "csv", "pdf", "txt", "ppt"];

        // 检查文件是否上传完成
        if ($file->isValid()) {
            // 检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                return $this->setError("请上传" . implode(",", $allowed_extensions) . "格式的文件");
            }

            //检测文件大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                return $this->setError("文件大小限制" . $maxSize . "M");
            }
        } else {
            return $this->setError($file->getErrorMessage());
        }
        $newFile = date('Y-m-d') . "_" . time() . "_" . uniqid() . "." . $file->getClientOriginalExtension();
        $disk = Storage::disk('oss');
        $disk->put($newFile, file_get_contents($file->getRealPath()));
        $image_id = $this->repository->insertGetId($newFile, $file->getClientSize(), $ext);
        return [
            'data' => $newFile,
            'file_id' => $image_id,
            'url' => $disk->getUrl($newFile)
        ];
    }
}
